﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using Lib.Common;
using Microsoft.ApplicationBlocks.Data;
using Module.EntitiesInfo;

namespace Module.DataAccess
{
    public class AnswerDB
    {
        #region ConnectionString
        private string _connectionString;
        public string ConnectionString
        {
            get { return _connectionString; }
            set { _connectionString = value; }
        }
        public AnswerDB()
        {
            ConnectionString = ConfigurationManager.ConnectionStrings["localsql"].ToString();
        }
        #endregion

        public virtual DataTable GetAllByQuestionId(int questionId)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {                              
                  new SqlParameter("@QuestionId", questionId)
            };

            using (DataTable dt = SqlHelper.ExecuteDataset(ConnectionString, CommandType.StoredProcedure, "Answers_GetAllbyQuestId", parameters).Tables[0])
            {
                return dt;
            }

        }
        public virtual DataTable GetAnswerUnCorrect(int top, int questionId, int bitMaskCorrect)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {                              
                  new SqlParameter("@Top", top),
                  new SqlParameter("@QuestionId", questionId),
                  new SqlParameter("@BitMaskCorrect", bitMaskCorrect)
            };

            using (DataTable dt = SqlHelper.ExecuteDataset(ConnectionString, CommandType.StoredProcedure, "Answers_GetAnswerUnCorrect", parameters).Tables[0])
            {
                return dt;
            }            
        }
        public virtual void Delete(int type, int id)
        {
            SqlParameter[] parameters = new SqlParameter[]
			{
                new SqlParameter("@type", type),
				new SqlParameter("@Id", id)
			};
            SqlHelper.ExecuteNonQuery(ConnectionString, CommandType.StoredProcedure, "Answers_Delete", parameters);
        }
        public virtual int Insert(AnswerInfo info)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
				new SqlParameter("@QuestionId", info.QuestionId),
				new SqlParameter("@Answer", info.Answer),
				new SqlParameter("@BitMaskPosition", info.BitMaskPosition),
                new SqlParameter("@IsCorrect",info.IsCorrect)
			};
            return (int)SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Answers_Insert", parameters);
        }

        public virtual void Update(AnswerInfo info)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Id", info.Id),
				new SqlParameter("@QuestionId", info.QuestionId),
				new SqlParameter("@Answer", info.Answer),				
                new SqlParameter("@IsCorrect",info.IsCorrect)
			};
            SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Answers_Update", parameters);
        }
        public virtual void UpdateStatus(int type, int id, int values)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Type", type),
				new SqlParameter("@Id", id),
				new SqlParameter("@Values", values)
			};
            SqlHelper.ExecuteScalar(ConnectionString, CommandType.StoredProcedure, "Questions_Update_Status", parameters);
        }

        public virtual IDataReader GetInfo(int id)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Id", id)				
			};
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Answers_GetInfo", parameters);
            
        }

        public virtual IDataReader GetAllByQId(int questionId)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@QuestionId", questionId)				
			};
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Answers_GetAllbyQuestId", parameters);           
        }

        public virtual IDataReader GetUncorrect(int top, int questionId)
        {
            SqlParameter[] parameters = new SqlParameter[]
            {
                new SqlParameter("@Top", top),
                new SqlParameter("@QuestionId", questionId)	
			};
            return SqlHelper.ExecuteReader(ConnectionString, CommandType.StoredProcedure, "Answers_GetUnCorrect", parameters);           
        }
    }
    
}

